<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        .clock-wrapper {
            width: 200px;
            margin: 50px auto;
            text-align: center;
        }

        .clock-wrapper .clock-time {
            font-size: 40px;
            line-height: 60px;
            height: 60px;
            font-family: "clockIcon";
        }

        .clock-wrapper .clock-date {
            font-size: 16px;
            line-height: 20px;
            height: 20px;
        }

        @font-face {
            font-family: "clockIcon";
            src: url("../font/clockicons.woff2");
        }

        .clock-title {
            display: flex;
            justify-content: space-evenly;
        }

        button {
            padding: 5px 10px;
        }

        .ms {
            font-size: 24px;
        }
    </style>
</head>

<body>
    <div class="clock-wrapper">
        <div class="clock-title">
            <button class="start">开始</button>
            <button class="pause">暂停</button>
            <button class="reset">重置</button>
        </div>
        <div class="clock-time">
            00:00<span class="ms">.00</span>
        </div>
    </div>
</body>
<script>
    var start = document.getElementsByClassName("start")[0];
    var pause = document.getElementsByClassName("pause")[0];
    var reset = document.getElementsByClassName("reset")[0];
    var clockTime = document.getElementsByClassName("clock-time")[0];


    // 函数防抖 => 在指定时间内连续触发,只执行最后一次


    var ms = 0;  // 全局变量毫秒(计时)
    var timer = null; // 计时器的编号

    // 传统的防抖写法   => 多了一个全局变量 dTimer
    // var dTimer = null;
    // start.onclick = function () {
    //     // 点击开始按钮  开始计时
    //     clearTimeout(dTimer);
    //     dTimer = setTimeout(function () { //将操作延迟300ms 
    //         // 执行的内容
    //         clearInterval(timer);  //启动下一个计时器之前 先清除上一个
    //         timer = setInterval(function () {
    //             ms += 10;
    //             // console.log(ms);

    //             var milli = ms % 1000 / 10;
    //             var second = parseInt(ms / 1000) % 60;
    //             var minute = parseInt(ms / 1000 / 60);

    //             clockTime.innerHTML = `${beauty(minute)}:${beauty(second)}<span class="ms">.${beauty(milli)}</span>`;
    //         }, 10);
    //     }, 300)
    // }

    var newFn = debounceFn(function () {
        clearInterval(timer);  //启动下一个计时器之前 先清除上一个
        timer = setInterval(function () {
            ms += 10;
            // console.log(ms);

            var milli = ms % 1000 / 10;
            var second = parseInt(ms / 1000) % 60;
            var minute = parseInt(ms / 1000 / 60);

            clockTime.innerHTML = `${beauty(minute)}:${beauty(second)}<span class="ms">.${beauty(milli)}</span>`;
        }, 10);
    }, 300);

    start.onclick = newFn;



    function debounceFn(callbackFn, delay = 0) {
        var dTimer = null;
        return function () {
            clearTimeout(dTimer);
            dTimer = setTimeout(function () {
                callbackFn();
            }, delay);
        }
    }








    pause.onclick = function () {
        clearInterval(timer);
    }
    reset.onclick = function () {
        clearInterval(timer);
        ms = 0;  // 全局变量毫秒(计时)
        timer = null;
        clockTime.innerHTML = `00:00<span class="ms">.00</span>`
    }
    function beauty(num) {
        return num < 10 ? "0" + num : num;
    }




</script>

</html>